package com.example.exception;

import com.example.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * GlobalExceptionHandler 全局异常处理
 *
 * @author 12427
 * @since 2025/6/7 8:52
 */
@Slf4j
@RestControllerAdvice//异常申明
public class GlobalExceptionHandler {
    @ExceptionHandler
    public Result headleException(Exception e){
        log.error("出错了",e);
        return Result.error("出错了~~~");
    }
    /**
     * 处理重复异常信息
     */
    public Result handleDuolicateKeyExction(DuplicateKeyException e){
        log.error("出错了",e);
        String message = e.getMessage();
        int i = message.indexOf("Duplicate entry");
        String errMsg = message.substring(i);
        String[] arr = errMsg.split("");
        return Result.error(arr[2]+"已经存在");
    }
}
